package mamikeji.liaowa.ui.activity.right;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MaxTextLength;

/**
 * Created by 廉冲-android开发 on 2017/5/9.
 */
public class UserFeedbackActivity extends BaseActivity {
    private static final String TAG = "UserFeedbackActivity";
    private TextView textView_feedback_wordnum;
    private EditText editText_feedback_cont;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_feedback);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.set_feedback_name);
        setTitleRightBtn(R.string.set_feedback_topright, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(editText_feedback_cont.getText().toString())) {
                    ForToast.showToast(R.string.set_feedback_error);
                    return;
                }
                getFeedback(editText_feedback_cont.getText().toString());
            }
        });
        init();
        initData();
    }

    /**
     * 加载布局控件
     */
    private void init() {
        textView_feedback_wordnum = (TextView) findViewById(R.id.textView_feedback_wordnum);
        editText_feedback_cont = (EditText) findViewById(R.id.editText_feedback_cont);

        InputFilter[] filters = new InputFilter[] { new MaxTextLength(400) };
        editText_feedback_cont.setFilters(filters);

        editText_feedback_cont.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                String lengthString = getResources().getString(R.string.publish_length);
                String lengthFinal = String.format(lengthString, s.length());
                textView_feedback_wordnum.setText(lengthFinal);
            }

        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        String lengthString = getResources().getString(R.string.publish_length);
        String lengthFinal = String.format(lengthString, 0);
        textView_feedback_wordnum.setText(lengthFinal);
    }

    /**
     * 意见反馈
     */
    private void getFeedback(String content){
        showLoading();
        Map<String, String> formMap = new HashMap<>();
        //后台信息要电话号码和usecode
        formMap.put(HttpKey.FEEDBACK_PHONE, ForPreference.getString(ForContents.USER_PHONE));
        formMap.put(HttpKey.FEEDBACK_USERCODE, ForPreference.getString(ForContents.USER_CODE));

//        formMap.put(HttpKey.FEEDBACK_PHONE, ForContents.USER_PHONE);
//        formMap.put(HttpKey.FEEDBACK_USERCODE, ForContents.USER_CODE);
        formMap.put(HttpKey.FEEDBACK_CONTENT, content);
        formMap.put(HttpKey.FEEDBACK_SOURCE, ForContents.GET_2);
        formMap.put(HttpKey.FEEDBACK_STYLE, "2");
        formMap.put(HttpKey.FEEDBACK_CRICLEID, "1");
        //增加了机型，App版本，系统版本字段
        formMap.put(HttpKey.FEEDBACK_VERSION, ForPreference.getString(ForContents.APP_VERSION));
        formMap.put(HttpKey.FEEDBACK_PHONESTYLE, ForPreference.getString(ForContents.USER_PHONE_MODEL));
        formMap.put(HttpKey.FEEDBACK_PHONESYSTEM, ForPreference.getString(ForContents.USER_PHONE_SYSTEM_LEVEL));

        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.SAVE_FEEDBACK, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG,e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                        finish();
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                    hideLoading();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
